var express = require('express');
var path = require('path');
var variables = require('./config/variables');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var eventsData = require('./config/charityEventsData');

var routes = require('./routes/index');
var users = require('./routes/users');
var articles = require('./routes/articles.js');
var security = require('./routes/security');
var project = require('./routes/project');
var resetPassword = require('./routes/resetPassword');
var admin = require('./routes/admin');
var audit = require('./routes/audit');
var dataAnalysis = require('./routes/dataAnalysis');
var bannerRouter = require('./routes/banners');
var thirdPartPay = require('./routes/thirdPartPay');

var adminPageRouter =  require('./routes/admin/adminRouter');
var messagesRouter = require('./routes/message');

var app = express();

global.CURRENT_ENV = app.get('env');

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser(variables.cookieSecret));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/articles', articles);
app.use('/api', security);
app.use('/projects', project);
app.use('/admin', admin);
app.use('/audits',audit);
app.use('/charityEventData', function(req,res,next){
    res.send(eventsData);
});
app.use('/banners',bannerRouter);

app.use('/dataAnalysis',dataAnalysis);

app.use('/pay',thirdPartPay);

app.use('/resetPassword',resetPassword);

app.use('/management', adminPageRouter);

app.use('/messages', messagesRouter);

app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.redirect('/#/error');
});

module.exports = app;
